﻿/* 
   Copyright 2011 LUCA Studios LLC & John Evans

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

namespace LUCAStudios.Silverlight.Controls
{
    using System;
    using System.Globalization;
    using System.Windows;

    /// <summary>
    /// Provides miscellaneous numeric helper extension methods
    /// </summary>
    public static class NumericHelpers
    {
        /// <summary>
        /// Returns true if integer is even or not 0, false otherwise
        /// </summary>
        /// <param name="num">the integer to check</param>
        /// <returns></returns>
        public static bool IsEven(this int num)
        {
            return ((num & 1) != 0);
        }

        /// <summary>
        /// Returns true if a given object is numeric, false otherwise.
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(this object expression)
        {
            if (expression == default(object)) return false;

            double number;
            return double.TryParse(
                Convert.ToString(expression, CultureInfo.InvariantCulture),
                NumberStyles.Any,
                NumberFormatInfo.InvariantInfo,
                out number);
        }

        /// <summary>
        /// Returns the Manhatten Distance between a given point and another point.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static double ManhattenDistanceTo(this Point from, Point to)
        {
            return Math.Abs(to.X - from.X) + Math.Abs(to.Y - from.Y);
        }
    }
}
